import axios, { type AxiosRequestHeaders, type AxiosResponse, type AxiosRequestConfig } from "axios";
// @ts-ignore
import NProgress from 'nprogress'
import "nprogress/nprogress.css";
import pinia from "@/stores";
import {useSource} from "@/stores/source";

//配置axios实例
const request = axios.create({
    // baseURL:import.meta.env.VITE_API_URL,
    baseURL:'http://zsjyyds.online:9000/apis/',
    timeout:5000
})

//添加请求拦截器
request.interceptors.request.use((config:AxiosRequestConfig)=>{
    NProgress.start()
    if (!useSource(pinia).isEleLoading){
        useSource(pinia).changeLoading(true)
    }
    //从pinia中传入token
    // (config.headers as AxiosRequestHeaders).token = useUserInfoStore(pinia).token;
    return config
})

//添加响应拦截器
request.interceptors.response.use((config:AxiosResponse)=>{
    NProgress.done()
    useSource(pinia).changeLoading(false)
    return config.data.data
})

export default request
